package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.service.impl.CustomerDeviceServiceImpl;
import org.example.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static org.example.enums.ResultCode.FAIL;
import static org.example.enums.ResultCode.TOKEN_NOT_EXIST;

@RestController
@RequestMapping("/openapi/v1.0/customerDevice")
@CrossOrigin
@Slf4j
public class CustomerDeviceController {
    @Autowired
    private CustomerDeviceServiceImpl customerDeviceService;

    @GetMapping("onlineDevice")
    @ResponseBody
    public Result getOnlineRate(@RequestParam(value = "customerTitle") String customerTitle,
                                @RequestHeader(value = "Authorization", required = false) String bearToken){
        System.out.println(bearToken);
        log.info("getting Online Rate... customerTitle:{}",customerTitle);
        if (bearToken == null) {
            return new Result(TOKEN_NOT_EXIST.code, false, null, "accessToken not exist");
        }
        if(customerTitle == null||customerTitle.isEmpty()){
            return new Result(FAIL.code, false, null, "Please input customerTitle");
        }

        String token=bearToken.substring(7,bearToken.length());
//        return customerDeviceService.getOnlineDevice(customerTitle,token);
        return customerDeviceService.getOnlineDeviceByEntityQuery(customerTitle,token);
    }


}
